"""A collection of temp Class Tanjiro"""


from src.common import config, settings, utils
import time
import math
from src.routine.components import Command
from src.common.vkeys import press, key_down, key_up


# List of key mappings
class TanjiroKey:
	# Movement
	JUMP = 'x' 
	FLASH_JUMP = 'x'
	High_Speed_Movement = 'v'

	# Buffs
	Warrior_Of_The_Corps = '1' 

	# Skills
	ThunderClap_And_Flash = 'e' 
	Whater_Wheel = 'a' 
	Whirlpool = 's'
	WaterFall_Basin = 'd' 
	
	# Bind
	Striking_Tide = 'w'
	
#########################
#       Commands        #
#########################

def step(direction, target):
	"""
	Performs one movement step in the given DIRECTION towards TARGET.
	Should not press any arrow keys, as those are handled by Auto Maple.
	"""

	num_presses = 2
	if direction == 'up' or direction == 'down':
		num_presses = 1
	if config.stage_fright and direction != 'up' and utils.bernoulli(0.75):
		time.sleep(utils.rand_float(0.1, 0.3))
	d_y = target[1] - config.player_pos[1]
	if abs(d_y) > settings.move_tolerance * 1.5:
		if direction == 'down':
			press(TanjiroKey.JUMP, 3)
		elif direction == 'up':
			press(TanjiroKey.JUMP, 1)
	press(TanjiroKey.FLASH_JUMP, num_presses)

class FlashJump(Command):
	"""Performs a flash jump in the given direction."""

	def __init__(self, direction):
		super().__init__(locals())
		self.direction = settings.validate_arrows(direction)

	def main(self):
		key_down(self.direction)
		time.sleep(0.1)
		press(TanjiroKey.FLASH_JUMP, 1)
		if self.direction == 'up':
			press(TanjiroKey.FLASH_JUMP, 1)
		else:
			press(TanjiroKey.FLASH_JUMP, 1)
		key_up(self.direction)
		time.sleep(0.5)

	
class Adjust(Command):
	"""Fine-tunes player position using small movements."""

	def __init__(self, x, y, max_steps=5):
		super().__init__(locals())
		self.target = (float(x), float(y))
		self.max_steps = settings.validate_nonnegative_int(max_steps)

	def main(self):
		counter = self.max_steps
		toggle = True
		error = utils.distance(config.player_pos, self.target)
		while config.enabled and counter > 0 and error > settings.adjust_tolerance:
			if toggle:
				d_x = self.target[0] - config.player_pos[0]
				threshold = settings.adjust_tolerance / math.sqrt(2)
				if abs(d_x) > threshold:
					walk_counter = 0
					if d_x < 0:
						key_down('left')
						while config.enabled and d_x < -1 * threshold and walk_counter < 60:
							time.sleep(0.05)
							walk_counter += 1
							d_x = self.target[0] - config.player_pos[0]
						key_up('left')
					else:
						key_down('right')
						while config.enabled and d_x > threshold and walk_counter < 60:
							time.sleep(0.05)
							walk_counter += 1
							d_x = self.target[0] - config.player_pos[0]
						key_up('right')
					counter -= 1
			else:
				d_y = self.target[1] - config.player_pos[1]
				if abs(d_y) > settings.adjust_tolerance / math.sqrt(2):
					if d_y < 0:
						FlashJump('up').main()
					else:
						key_down('down')
						time.sleep(0.05)
						press(TanjiroKey.JUMP, 3, down_time=0.1)
						key_up('down')
						time.sleep(0.05)
					counter -= 1
			error = utils.distance(config.player_pos, self.target)
			toggle = not toggle
			
class Buff(Command):
	"""Uses each of Shadowers's buffs once."""

	def __init__(self):
		super().__init__(locals())
		self.cd120_buff_time = 0
		self.cd180_buff_time = 0
		self.cd200_buff_time = 0
		self.cd240_buff_time = 0
		self.cd900_buff_time = 0
		self.decent_buff_time = 0

	def main(self):
		buffs = [TanjiroKey.Warrior_Of_The_Corps]
		now = time.time()

		if self.cd120_buff_time == 0 or now - self.cd120_buff_time > 120:
			# press(ShadowKey.EPIC_ADVENTURE, 2)
			self.cd120_buff_time = now
		if self.cd180_buff_time == 0 or now - self.cd180_buff_time > 180:
			press(TanjiroKey.Warrior_Of_The_Corps, 1)
			self.cd180_buff_time = now
		if self.cd200_buff_time == 0 or now - self.cd200_buff_time > 200:
			# press(ShadowKey.SHADOW_PARTNER, 2)
			self.cd200_buff_time = now
		if self.cd240_buff_time == 0 or now - self.cd240_buff_time > 240:
			self.cd240_buff_time = now
		if self.cd900_buff_time == 0 or now - self.cd900_buff_time > 900:
			# press(ShadowKey.MAPLE_WARRIOR, 2)
			self.cd900_buff_time = now
		# if self.decent_buff_time == 0 or now - self.decent_buff_time > settings.buff_cooldown:
			# for key in buffs:
			# 	press(key, 3, up_time=0.3)
			# self.decent_buff_time = now

class Whater_Wheel(Command):
	# use Whater_Wheel once
	
	def main(self):
		press(TanjiroKey.Whater_Wheel, 1)

class Whirlpool(Command):
		# use Whirlpool once
	
	def main(self):
		press(TanjiroKey.Whirlpool, 1)

class WaterFall_Basin(Command):
		# use WaterFall_Basin once
	
	def main(self):
		press(TanjiroKey.WaterFall_Basin, 1)
		
class ThunderClap_And_Flash(Command):
	# use ThunderClap_And_Flash once cd 180
	def __init__(self):
		super().__init__(locals())
		self.cd_skill_time = 180
		self.record_skill_time = 0

	def main(self):
		now = time.time()
		if self.record_skill_time == 0 or now - self.record_skill_time > self.cd_skill_time:
			press(TanjiroKey.ThunderClap_And_Flash, 1)
			self.record_skill_time = now